//
//  AppDelegate.m
//  FoodFamily
//
//  Created by Bang-guo on 15-6-19.
//  Copyright (c) 2015年 guoBang. All rights reserved.
//

#import "AppDelegate.h"
#ifdef DEBUG
#include <mach/mach.h>
#endif

@interface AppDelegate ()

@end

@implementation AppDelegate
-(void)dealloc {
    
    [_window release];
    [super dealloc];
}
#ifdef DEBUG
BOOL memoryInfo(vm_statistics_data_t *vmStats) {
    mach_msg_type_number_t infoCount = HOST_VM_INFO_COUNT;
    kern_return_t kernReturn = host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)vmStats, &infoCount);
    
    return kernReturn == KERN_SUCCESS;
}

- (void)logMemoryInfo {
    vm_statistics_data_t vmStats;
    
    if (memoryInfo(&vmStats)) {
        
        self.memoryLabel.text = [NSString stringWithFormat:@"free:%luM,use:%luM"
                                 ,vmStats.free_count * vm_page_size / 1024 / 1024,
                                 vmStats.active_count * vm_page_size / 1024 / 1024];
        NSLog(@"%@", self.memoryLabel.text);
    }
}
#endif


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    [NSThread sleepForTimeInterval:2.0];//设置启动页面时间
    
    
    // Override point for customization after application launch.
//    设置状态栏的字体颜色
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];
    
    FoodCityViewController *foodCityVC = [[FoodCityViewController alloc] init];
    UINavigationController *naFoodCity = [[UINavigationController alloc] initWithRootViewController:foodCityVC];
    naFoodCity.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"美食city" image:[UIImage imageNamed:@"footCity.png"] tag:10001];
    
    FoodAboutViewController *foodAboutVC = [[FoodAboutViewController alloc] init];
    UINavigationController *naFoodAbout = [[UINavigationController alloc] initWithRootViewController:foodAboutVC];
    naFoodAbout.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"美食about" image:[UIImage imageNamed:@"footAbout.png"] tag:10002];
    
    FoodVideoViewController *foodVideoVC = [[FoodVideoViewController alloc] init];
    UINavigationController *naFoodVideo = [[UINavigationController alloc] initWithRootViewController:foodVideoVC];
    naFoodVideo.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"美食Video" image:[UIImage imageNamed:@"footFamily.png"] tag:10003];
    
    SettingViewController *settingVC = [[SettingViewController alloc] init];
    UINavigationController *naSetting = [[UINavigationController alloc] initWithRootViewController:settingVC];
    naSetting.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"设置" image:[UIImage imageNamed:@"setting.png"] tag:10004];
    
    UITabBarController *tabBarC = [[UITabBarController alloc] init];
    tabBarC.tabBar.translucent = NO;
    tabBarC.viewControllers = [NSArray arrayWithObjects:naFoodCity, naFoodAbout, naFoodVideo, naSetting, nil];
 
    self.window.rootViewController = tabBarC;
    [_window release];
    [foodCityVC release];
    [foodAboutVC release];
    [foodVideoVC release];
    [settingVC release];
    [naFoodCity release];
    [naFoodAbout release];
    [naFoodVideo release];
    [naSetting release];
    
//#pragma 开机动画
//    UIImageView *view1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.window.frame.size.width , self.window.frame.size.height/2)];
//    view1.backgroundColor = [UIColor orangeColor];
//    view1.image = [UIImage imageNamed:@"wwwww.jpg"];
//    [self.window addSubview:view1];
//    
//    UIImageView *view2 = [[UIImageView alloc] initWithFrame:CGRectMake(0,self.window.frame.size.height/2, self.window.frame.size.width , self.window.frame.size.height/2)];
//    view2.backgroundColor = [UIColor orangeColor];
//    view2.image = [UIImage imageNamed:@"wwwww.jpg"];
//    [self.window addSubview:view2];
//    
//    
//    
//    [UIView animateWithDuration:2 delay:1 usingSpringWithDamping:1 initialSpringVelocity:0.5 options:UIViewAnimationOptionLayoutSubviews animations:^{
//        
//        view1.frame = CGRectMake(0, -self.window.frame.size.height, self.window.frame.size.width , self.window.frame.size.height/2);
//        view2.frame = CGRectMake(0,self.window.frame.size.height, self.window.frame.size.width , self.window.frame.size.height/2);//
//        
//    } completion:^(BOOL finished) {
//        
//        [view1 removeFromSuperview];
//        [view2 removeFromSuperview];
//        
//        
//    }];


    
#warning 城市数据库 添加城市
//    [[MapCity mapCityShareDataHandle] dropMapCityTable];
    
    NSMutableArray *array = [[MapCity mapCityShareDataHandle] selectMapCityAll];
    NSLog(@"array.count = %ld", array.count);
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        if (array.count == 0) {
            [[MapCity mapCityShareDataHandle] createMapCityTable];
            [[MapCity mapCityShareDataHandle] insertMapCityValue];
        }
    
    });
    
//#ifdef DEBUG
//    //debug时用来检测内存使用情况
//    self.memoryLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, 320, 25)];
//    self.memoryLabel.backgroundColor = [UIColor greenColor];
//    self.memoryLabel.alpha = 0.5;
//    self.memoryLabel.textAlignment = NSTextAlignmentCenter;
//    [self.window addSubview:self.memoryLabel];
//    
//    [NSTimer scheduledTimerWithTimeInterval:1. target:self selector:@selector(logMemoryInfo) userInfo:nil repeats:YES];
//#endif
    return YES;
    
   }

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
